﻿@{
    var tableName = PageViewModel.PageTableID(this);
    var orgTreeName = PageViewModel.ControlID(this, "ul_org_tree");
    var orgTreeMenu = PageViewModel.ControlID(this, "ul_org_tree_menu");

    var onSelectOrgNode = PageViewModel.PageFunctionName(this, "on_select_org");
    var onOrgContextMenuNode = PageViewModel.PageFunctionName(this, "on_orm_context_menu");
    var org_node_fmt = PageViewModel.PageFunctionName(this, "org_node_fmt");

    var newOrgFun = PageViewModel.PageFunctionName(this, "new_org_fun");
    var editOrgFun = PageViewModel.PageFunctionName(this, "edit_org_fun");
    var editOrgDlgName = PageViewModel.PageEditDialog(this, "edit_org_fun", "_dlg");
    var saveOrgFunName = PageViewModel.PageFunctionName(this, "save_edit_org");
    var deleteOrgFunName = PageViewModel.PageFunctionName(this, "delete_org");
}

<style type="text/css">
    #@orgTreeName .tree-node {  height:32px;line-height:32px; }
    #@orgTreeName .tree-node .tree-file{  background-image: none; }
    #@orgTreeName .tree-node .tree-folder{  background-image: none; }
    #@orgTreeName .tree-node .tree-folder-open{  background-image: none; }
</style>

<script type="text/javascript">
    function @onSelectOrgNode (node) {
        $('#@tableName').treegrid('load', {
            OrganizationID: node.Id,
        });
    }

    function @onOrgContextMenuNode (e,node) {
        e.preventDefault();
        $('#@orgTreeName').tree('select', node.target);
        $('#@orgTreeMenu').menu('show', {
    	    left: e.pageX,
    	    top: e.pageY
        });
    }
</script>
<script type="text/javascript">
    function @editOrgFun () {
        var node = $('#@orgTreeName').tree('getSelected');
        if (node != null) {
            @newOrgFun (node.Id);
        }
    }

    function @newOrgFun (id) {
        var dlg = $dlg('@editOrgDlgName');
        dlg.dialog({
            title: '组织机构', width: 600, height: 420,
            closed: false, cache: false, modal: true, maximizable: false, resizable: true,
            href: '~/Organization/_EditOrganization?NOT_ID=' + id,
            onResize: function () {
                $(this).dialog('center');
            },
            buttons: [
                {
                    text: "保 存",
                    handler: function () { @saveOrgFunName (); },
                    iconCls: "icon-save"
                }, {
                    text: "取 消",
                    handler: function () { $("#@editOrgDlgName").dialog("close"); },
                    iconCls: "icon-cancel"
                }
            ],
            onLoad: function () {
                LoadObject(id, "~/Organization/GetOrganizationByID/", function (org) {
                    var form = $("#@editOrgDlgName").find("form");
                    form.form("load", org);
                });
            }
        });
    }

    function @saveOrgFunName () {
            var form = $("#@editOrgDlgName").find("form");
            if (form.form('enableValidation').form('validate')) {
                var org = form.form_to_json();

                $.ajax({
                    url: "~/Organization/SaveOrganization",
                    type: "POST",
                    data: org,
                    success: function (e) {
                        if (e.Result) {
                            $.messager.alert('保存成功', '保存成功!', 'info', function () {
                                $('#@editOrgDlgName').dialog('close');
                                $('#@orgTreeName').tree('reload');
                            });
                        } else {
                            $.messager.alert('保存失败', '保存失败: ' + e.Msg, 'error');
                        }
                    },
                    error: function (e) {
                        ShowAjaxError(e);
                    }
                });
            }
    }

    function @deleteOrgFunName () {
        var node = $('#@orgTreeName').tree('getSelected');
        if (node != null) {
            if (node.Id > 0 && confirm("确认删除组织机构信息?")) {
                $.ajax({
                    async: false,
                    method: "DELETE",
                    url: $.Content("~/Organization/DeleteOrganization/" + node.Id),
                    context: $("#@orgTreeName"),
                    success: function (e) {
                        if (e.Result) {
                            this.tree('reload');
                        } else {
                            $.messager.alert('删除数据错误', '删除失败: ' + e.Msg, 'error');
                        }
                    },
                    error: function (e1, e2, e3) {
                        ShowAjaxError(e1, "删除数据错误");
                        this.tree('reload');
                    }
                });
            }
        }
    }
</script>

<ul id="@orgTreeName" class="easyui-tree" fit="true" data-options="url:'~/Organization/GetOrganizationTree',method:'get',
                                                                               onSelect:@onSelectOrgNode,
                                                                               onContextMenu:@onOrgContextMenuNode"></ul>
<div id="@orgTreeMenu" class="easyui-menu" style="width:120px;">
    <div onclick="@newOrgFun ()" data-options="iconCls:'icon-add'">新增</div>
    <div onclick="@editOrgFun ()" data-options="iconCls:'icon-edit'">修改</div>
    <div onclick="@deleteOrgFunName ()" data-options="iconCls:'icon-remove'">删除</div>
</div>